package com.atguigu.day09;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink07_SQL_KafkaToKafka {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        //获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);


        //2.创建一张表，映射到Kafka的某个Topoic上，然后消费数据
        tableEnv.executeSql("create table kafkaSource(id String,ts bigint,vc int) with(" +
                "'connector' = 'kafka'," +
                "'topic' = 'topic_source_sensor'," +
                "'properties.bootstrap.servers' = 'hadoop102:9092'," +
                "'properties.group.id' = '0409'," +
                "'scan.startup.mode' = 'latest-offset'," +
                "'format' = 'csv'" +
                ")");


        //创建一张表，映射到kafka的某个topic上，然后生成数据
        tableEnv.executeSql("create table kafkaSink(id String,ts bigint,vc int) with(" +
                "'connector' = 'kafka'," +
                "'topic' = 'topic_sink_sensor'," +
                "'properties.bootstrap.servers' = 'hadoop102:9092'," +
                "'format' = 'json'" +
                ")");

        //读取kafkaSource中的数据写入kafkaSink表中
        tableEnv.executeSql("insert into kafkaSink select * from kafkaSource where id = 'sensor_1'");
    }
}
